#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#include <errno.h>
#include <sys/sem.h>
#include <unistd.h>
#include "sem.h"
#include "cgic.h"
#include "data_global.h"

#define N 1024

char status[2][6] = {"Close", "Open"};
char fan_status[4][6] = {"Close", "One", "Two", "Three"};

struct shm_addr
{
    char shm_status;
    struct env_info_client_addr  sm_all_env_info;
};

int cgiMain()
{
	key_t key;
	int shmid,semid;
	struct shm_addr *shm_buf;
	
	
	if((key = ftok("/tmp",'i')) <0)
	{
		perror("ftok");
		exit(1);
	}
	// printf("key = %x\n",key);

	if((semid  = semget(key, 1, 0666)) < 0)
	{
		perror("semget");
		exit(1);
	}

	if((shmid = shmget(key, N, 0666 )) == -1)
	{
		perror("shmget");
		exit(1);
	}

	if((shm_buf = (struct shm_addr*)shmat(shmid, NULL, 0)) == (void*)-1 )
	{
		perror("shmat");
		exit(1);
	}

	sem_p(semid,0);

	cgiHeaderContentType("text/html");

	fprintf(cgiOut, "<head><meta http-equiv=\"refresh\" content=\"1\"><style><!--body{line-height:50%%}--></style> </head>");
	fprintf(cgiOut, "<HTML>\n");
	fprintf(cgiOut, "<BODY bgcolor=\"#666666\">\n");
	// fprintf(cgiOut, "<h1><font color=\"#FF0000\">HOME_ID #%d:</font></H2>\n ", shm_buf->shm_status);
	if (shm_buf->shm_status)
	{
		fprintf(cgiOut, "<script>function show(){var date =new Date(); var now = \"\"; now = date.getFullYear()+\"年\"; now = now + (date.getMonth()+1)+\"月\"; now = now + date.getDate()+\"日\"; now = now + date.getHours()+\"时\"; now = now + date.getMinutes()+\"分\";now = now + date.getSeconds()+\"秒\"; document.getElementById(\"nowDiv\").innerHTML = now; setTimeout(\"show()\",1000);} </script> \n ");	
		fprintf(cgiOut, "<h2><font face=\"Broadway\"><font color=\"#00FAF0\">Home1 Real-time Environment Info:</font></font></H2>\n ");
		fprintf(cgiOut, "<h2 align=center><font color=\"#cc0033\"><body onload=\"show()\"> <div id=\"nowDiv\"></div></font></h2> \n "); 	
		struct makeru_env_data now_data = shm_buf->sm_all_env_info.monitor_no[(int)shm_buf->shm_status];
		fprintf(cgiOut, "<h4>key = %x</H4>\n ",key);
		fprintf(cgiOut, "<h4>bufsize = %d</H4>\n ", sizeof(*shm_buf));
		fprintf(cgiOut, "<h4>datasize = %d</H4>\n ", sizeof(now_data));
		if(crc16_check((uint8_t*)&now_data.a9_info, sizeof(now_data.a9_info)) && crc16_check((uint8_t*)&now_data.zigbee_info, sizeof(now_data.zigbee_info)))
		{
			fprintf(cgiOut, "<h4>ZIGBEE数据显示部分</H4>\n ");
			fprintf(cgiOut, "<h4>Temperature:\t%0.2f℃</H4>\n ", ((struct makeru_zigbee_info*)&now_data.zigbee_info.buffer)->temperature );
			fprintf(cgiOut, "<h4>Humidity:\t%0.2f%%</H4>\n ", ((struct makeru_zigbee_info*)&now_data.zigbee_info.buffer)->humidity);
			
			fprintf(cgiOut, "<h4>A9数据显示部分</H4>\n ");
			fprintf(cgiOut, "<h4>Adc:\t%0.2f</H4>\n ", ((struct makeru_a9_info*)&now_data.a9_info.buffer)->adc);
			fprintf(cgiOut, "<h4>GYROX:\t%.2f°/s</H4>\n ", ((struct makeru_a9_info*)&now_data.a9_info.buffer)->gyrox);
			fprintf(cgiOut, "<h4>GYROY:\t%.2f°/s</H4>\n ", ((struct makeru_a9_info*)&now_data.a9_info.buffer)->gyroy);
			fprintf(cgiOut, "<h4>GYROZ:\t%.2f°/s</H4>\n ", ((struct makeru_a9_info*)&now_data.a9_info.buffer)->gyroz);
			fprintf(cgiOut, "<h4>AACX :\t%.2fg</H4>\n ", ((struct makeru_a9_info*)&now_data.a9_info.buffer)->accx);
			fprintf(cgiOut, "<h4>AACY :\t%.2fg</H4>\n ", ((struct makeru_a9_info*)&now_data.a9_info.buffer)->accy);
			fprintf(cgiOut, "<h4>AACZ :\t%.2fg</H4>\n ", ((struct makeru_a9_info*)&now_data.a9_info.buffer)->accz);
			fprintf(cgiOut, "<h4>A9-RESERVED[0]:\t%d</H4>\n ", ((struct makeru_a9_info*)&now_data.a9_info.buffer)->reserved[0]);
			fprintf(cgiOut, "<h4>A9-RESERVED[1]:\t%d</H4>\n ", ((struct makeru_a9_info*)&now_data.a9_info.buffer)->reserved[1]);
			
			fprintf(cgiOut, "<h4>STM32数据显示部分</H4>\n ");
			fprintf(cgiOut, "<h4>......</H4>\n ");
		}
		else
		{
			fprintf(cgiOut, "<h2><font face=\"Broadway\"><font color=\"#FFFAF0\">Close!</font></font></H2>\n ");
		}
	}
	else
	{
		fprintf(cgiOut, "<h2><font face=\"Broadway\"><font color=\"#FFFAF0\">Close!</font></font></H2>\n ");
	}
//	fprintf(cgiOut, "<h3>:</H3>\n ");
	fprintf(cgiOut, "</BODY></HTML>\n");	
	sem_v (semid, 0);
	return 0;
}


 



